package com.example.lab.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.lab.entity.Colleges;
import com.example.lab.entity.Laboratories;
import com.example.lab.mapper.CollegesMapper;
import com.example.lab.mapper.LaboratoriesMapper;
import com.example.lab.result.PageResult;
import com.example.lab.service.ICollegesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 学院信息表 服务实现类
 * </p>
 *
 * @author 布莱恩
 * @since 2025-06-17
 */
@Service
@RequiredArgsConstructor
public class CollegesServiceImpl extends ServiceImpl<CollegesMapper, Colleges> implements ICollegesService {

    private final CollegesMapper collegesMapper;
    private final LaboratoriesMapper laboratoriesMapper;

    @Override
    public PageResult<Colleges> getColleges(Integer page, Integer limit) {

        Page<Colleges> pageParam = new Page<>(page, limit);
        Page<Colleges> collegePage = baseMapper.selectPage(pageParam, new QueryWrapper<Colleges>());

        return new PageResult<>(
                collegePage.getRecords(),
                collegePage.getTotal(),
                collegePage.getCurrent(),
                collegePage.getSize()
        );
    }

    @Override
    public Colleges getCollegeById(Integer collegeId) {
        return baseMapper.selectById(collegeId);
    }

    @Override
    public boolean createCollege(Colleges college) {
        // 检查学院名称是否已存在
        QueryWrapper<Colleges> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("college_name", college.getCollegeName());
        if (baseMapper.selectCount(queryWrapper) > 0) {
            throw new RuntimeException("学院名称已存在");
        }

        return baseMapper.insert(college) > 0;
    }

    @Override
    public boolean updateCollege(Integer collegeId, Colleges college) {
        college.setCollegeId(collegeId);

        // 检查学院名称是否与其他记录冲突
        QueryWrapper<Colleges> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("college_name", college.getCollegeName())
                .ne("college_id", collegeId);
        if (baseMapper.selectCount(queryWrapper) > 0) {
            throw new RuntimeException("学院名称已被其他学院使用");
        }

        return baseMapper.updateById(college) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteCollege(Integer collegeId) {
        // 检查是否有实验室关联该学院
        if (hasLaboratories(collegeId)) {
            throw new RuntimeException("该学院下存在实验室，无法删除");
        }

        return baseMapper.deleteById(collegeId) > 0;
    }


    /**
     * 检查学院下是否存在实验室
     * @param collegeId 学院ID
     * @return true-存在实验室，false-不存在实验室
     */
    private boolean hasLaboratories(Integer collegeId) {
        QueryWrapper<Laboratories> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("college_id", collegeId);
        return laboratoriesMapper.selectCount(queryWrapper) > 0;
    }
}
